#!/usr/bin/env perl

#
# Copyright (c) 2002-2004 Eric Wallengren
# This file is part of the Continuous Automated Build and Integration
# Environment (CABIE)
#
# CABIE is distributed under the terms of the GNU General Public
# License version 2 or any later version.  See the file COPYING for copying
# permission or http://www.gnu.org.
#
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR
# IMPLIED, without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  ANY USE IS AT YOUR OWN RISK.
#
# Permission to modify the code and to distribute modified code is granted,
# provided the above notices are retained, and a notice that the code was
# modified is included with the above copyright notice.
#

BEGIN {
    push @INC, "lib";
    push @INC, "../lib";
    push @INC, "../../lib";
}

use strict;
use Sys::Hostname;

my $ospackage;
my $hostname = hostname();
$hostname    =~ s/\.[a-zA-Z0-9\n]+//g;
$hostname    =~ s/-//g;

if ($ =~ /MSWin32/) {
    $ospackage = "winsys";
} else {
    $ospackage = "unixsys";
}

require "$hostname.pm";
require "$ospackage.pm";

my $server    = $ARGV[0];
my $buildname = $ARGV[1];
my $buildnum  = $ARGV[2];
my $extras    = $ARGV[3];

my %mainhash;
my $key;

my $submit;
my $numrecs  = 0;
my $noextras = 0;

if (!defined($buildname) || !defined($buildnum) || !defined($server)) {
    useage();
    exit -1;
}

if (!defined($extras)) {
    $noextras = 1;
    $extras = ".";
}

if (defined($extras)) {
    $noextras = 1;
}

#
# We need to use classes from these files.
#
my $os   = new $ospackage;
my $conf = new $hostname;

my $existquery;
my @existarray;

$existquery = "select comment from comments where binary ".
              "server=\"$server\" and binary title=\"$buildname\" ".
              "and job=\"$buildnum\"";

@existarray = $os->run_sql_query($existquery, ";");

my $existnum = @existarray;

my $port = _getport("$server", "$buildname");

my $sccs = _getsccs("$server", "$buildname");

my @contents = _getlogs("$server", "$buildname", "$buildnum");

$submit = "Defects fixed in this build:\n";

foreach $key (keys %mainhash) {
    $submit .= "[$key], ";
    $numrecs++;
}

$submit =~ s/, $//g;

if ($numrecs > 0) {
    _addrecord("$server", "$buildname", "$buildnum", "$submit", "$extras");
}

if ($numrecs == 0 && defined($extras) && $noextras) {
    $submit = "Information:\n";
    _addrecord("$server", "$buildname", "$buildnum", "$submit", "$extras");
}

sub _addrecord {

    my $server  = shift;
    my $name    = shift;
    my $num     = shift;
    my $message = shift;
    my $extra   = shift;

    my @sqlarray;
    my $sqlquery;

    if ($existnum == 0) {

        push @sqlarray, "$server";
        push @sqlarray, "$name";
        push @sqlarray, "$num";
        push @sqlarray, "$message$extra";
    
        _logger( "submitting @sqlarray" );
    
        if (($os->run_sql_submit("comments", @sqlarray)) == 0) {
           _logger( "failed to write record to comments table" );
        }

    } else {

        _logger( "existnum: $num" );
        _logger( "existarray: $existarray[0]" );
        

        $sqlquery = "update comments set ".
                    "comment=\"$message$extra $existarray[0]\" ".
                    "where binary server=\"$server\" and ".
                    "binary title=\"$name\" and job=\"$num\"";

        _logger( "Running $sqlquery" );

        @sqlarray = $os->run_sql_query($sqlquery, ";");

        _logger( "Past sqlquery====" );

    }
    
    
}

sub _getsccs {

    my $server = shift;
    my $name   = shift;

    my $sqlquery;
    my @sqlarray;

    $sqlquery  = "select sccs from configuration where ".
                 "binary server=\"$server\" and binary ".
                 "title=\"$name\"";
    
    @sqlarray  = $os->run_sql_query("$sqlquery", ";", 0);

    return $sqlarray[0];

}

sub _getport {

    my $server = shift;
    my $name   = shift;

    my $sqlquery;
    my @sqlarray;

    $sqlquery  = "select port from configuration where ".
                 "binary server=\"$server\" and binary ".
                 "title=\"$name\"";
    
    @sqlarray  = $os->run_sql_query("$sqlquery", ";", 0);

    return $sqlarray[0];

}

sub _getlogs {

    my $server = shift;
    my $name   = shift;
    my $num    = shift;

    my $p4user = $conf->P4USER;
    my $p4pass = $conf->P4PASSWD;

    my $sqlquery;
    my @sqlarray;

    my @return;

    my $line;
    my $entry;

    $sqlquery  = "select changes from changes where binary server=\"$server\" ".
                 "and binary title=\"$name\" and job=\"$num\" ".
                 "order by changes";

    @sqlarray  = $os->run_sql_query("$sqlquery", ";", 0);

    if (@sqlarray > 0) {
        _logger( "processing filelist" );
        foreach $line (@sqlarray) {
            my @log; 
            my ($name, $ver, $date, $who) = split(/;/, $line);
            _logger( "Searching $name $ver for comments" );
            if ($sccs =~ /^cvs$/) {
                @log = `cvs -d $port rlog -N -r$ver $name`;
            }
            if ($sccs =~ /^perforce$/) {
                @log = `p4 -P $p4pass -u $p4user -p $port describe -s $date`;
            }
            foreach $entry (@log) {
                my ($left, $right)   = split(/\[/, $entry);
                my ($nleft, $nright) = split(/\]/, $right);
                if ($nleft =~ /^[0-9]/) {
                    $mainhash{$nleft} = "";
                }
            }
        }
    }

}

sub _logger {

    my $string = shift;

    my $formattime;
    my $reqtime = scalar localtime;

    print "[$reqtime]: $string\n";

}

sub useage {

    print "getlogs.pl servername jobname jobnum\n";

}
